package com.domain.controller;

import com.domain.common.Comments;
import com.domain.common.Result;
import com.domain.pojo.SmsCode;
import com.domain.service.H5Service;
import com.domain.service.SmsCodeService;
import com.domain.utils.NotNullUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/h5")
public class H5Controller {
    @Autowired
    private H5Service h5Service;

    @Autowired
    private SmsCodeService smsCodeService;


    /**
     * 获取短信验证码(阿里短信)
     *
     * @param phoneNum //手机号码
     */
    @RequestMapping(value = "/getsmsCode", method = RequestMethod.GET)
    @ResponseBody
    public Result getsmsCode(String phoneNum) {
        try {
            String code = "7676";
            /*     AliyunSmsUtils.sendSmsResgster(phoneNum, code);*/
            smsCodeService.saveSmsCode(phoneNum, code);
            return Result.success("验证码", code);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("短信验证码获取失败");
        }
    }

    /**
     * 获取帮忙详情
     *
     * @param helpUserId
     * @param publishId
     * @return
     */
    @RequestMapping(value = "/getH5PublishDetail", method = RequestMethod.GET
    )
    @ResponseBody
    public Result getH5PublishDetail(String helpUserId, String publishId, String helpType) {
        try {
            return h5Service.getH5PublishDetail(helpUserId, publishId, helpType);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("操作失败");
        }
    }

    /**
     * h5注册
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/h5Regester", method = RequestMethod.GET)
    @ResponseBody
    public Result h5Regester(String phoneNumber, String smsCode, String helpUsersId) {
        try {
            //验证验证码码得时效性（五分钟）
            SmsCode sc = smsCodeService.checkSmsCodeTime(phoneNumber, smsCode);
            if (!NotNullUtils.isNotNullObject(sc)) {
                return Result.publicResult(Comments.SMSCODE_FAIL, "验证码错误");
            } else {
                Long s = (System.currentTimeMillis() - sc.getCreatTime().getTime()) / (1000 * 60);
                if (s >= 5) {
                    //过期删除验证码
                    smsCodeService.delect(sc.getId());
                    return Result.publicResult(Comments.SMSCODE_CHECK, "验证码已过期！");
                }
            }
            return h5Service.h5Regester(phoneNumber, helpUsersId, smsCode);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

}
